import request from '@/utils/request'
import { SET_ARTICLES } from '../actionTypes'

export const getArticle = (params) => {
  return async (dispatch) => {
    // 发ajax请求 拿数据
    const res = await request({
      url: '/mp/articles',
      method: 'GET',
      params
    })
    console.log('getarticles', res.data)
    // // 2. 保存token到redux
    dispatch({ type: SET_ARTICLES, payload: res.data })
  }
}
// 删除功能
export const delAritcle = (id) => {
  return async (dispatch) => {
    await request({
      url: '/mp/articles/' + id,
      method: 'DELETE'
    })
  }
}
// 发布文章 / 添加草稿
export const addArticle = (data, draft = false) => {
  return async (dispatch) => {
    await request({
      url: '/mp/articles',
      method: 'post',
      data,
      params: { draft }
    })
  }
}
// 编辑文章 / 修改草稿
export const editArticle = (data, draft = false) => {
  return async (dispatch) => {
    await request({
      url: '/mp/articles/' + data.id,
      method: 'put',
      data,
      params: { draft }
    })
  }
}
// 路由跳转  +携带id
export const getArticleInfo = (id) => {
  return async (dispatch) => {
    const res = await request({
      url: '/mp/articles/' + id,
      method: 'GET'
    })
    console.log('文章详情', res.data)
    return res.data
  }
}
